import 'dart:io';

import 'package:app/common/colors.dart';
import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:app/utils/uint.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:bruno/bruno.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_pickers/image_pickers.dart';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:get/route_manager.dart';

class ManageFeedBackPage extends StatefulWidget {
  @override
  _ManageFeedBackPageState createState() => _ManageFeedBackPageState();
}

class _ManageFeedBackPageState extends State<ManageFeedBackPage>
    with TickerProviderStateMixin {


  String? qiniu_image_path, qiniu_image_path1, qiniu_image_path2;

  /// 当前选择的文件
  PlatformFile? selectedFile;

  TextEditingController editingController = TextEditingController();

  var listFilePaths = [];

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  
  requestData()async{

    MultipartFile? multipartFile;

    if(listFilePaths.length>0){
       multipartFile = MultipartFile.fromFileSync(
        '${listFilePaths[0]['path']}',filename: '${listFilePaths[0]['name']}',
      );

    }
    var formData = FormData.fromMap({
      'name':editingController.text,
      'file':multipartFile,
      // 'state':'未阅',
      // 'tomorrow_content':'',
    });
    var json = await DioManager().kkRequest(Address.helpdeskCreate,bodyParams: formData,isShowLoad:true);
    if(json['errmsg'] == 'ok'){
      Get.back();
    }
    BotToast.showText(text: json['message']);
  }

  ///选择图片
  Future<void> selectImages() async {
    try {

      // listFilePaths = await ImagePickers.pickerPaths(
      //   galleryMode: _galleryMode,
      //   showGif: false,
      //   selectCount: 1,
      //   showCamera: false,
      //   cropConfig: CropConfig(enableCrop: true, height: 1, width: 1),
      //   compressSize: 300,
      //   uiConfig: UIConfig(
      //     uiThemeColor: AppColor.themeColor,
      //   ),
      // );
      ImagePickers.pickerPaths(
        galleryMode: GalleryMode.image,
        showGif: false,
        selectCount: 1,
        showCamera: false,
        cropConfig: CropConfig(enableCrop: true, height: 1, width: 1),
        compressSize: 300,
        uiConfig: UIConfig(
          uiThemeColor: AppColor.themeColor,
        ),
      ).then((value) {
        var fileParams = {
          'name':'${value[0].path}',
          'path':'${value[0].path}',
        };
        listFilePaths.add(fileParams);
        setState(() {

        });

      });
      if (listFilePaths.length > 0) {

      }
      setState(() {

      });

    } on PlatformException {}
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: BaseAppBar(
          child: Text('问题反馈'),
          preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
          action: [
            TextButton(onPressed: (){
              if(editingController.text.isEmpty){
                BotToast.showText(text: '内容不能为空');
                return;
              }
              if(editingController.text.length<10){
                BotToast.showText(text: '不能少于10字');
                return;
              }
              requestData();
            }, child: Text('提交',style: TextStyle(color: Colors.white),))
          ],
        ),
        body: Container(
            color: Colors.white,
            padding: const EdgeInsets.all(5.0),
            child: Column(
              children: [
                BrnInputText(
                  maxHeight: 200,
                  minHeight: 30,
                  minLines: 1,
                  maxLength: 50,
                  bgColor: Colors.white,
                  textInputAction: TextInputAction.newline,
                  maxHintLines: 20,
                  textEditingController: editingController,
                  hint: '请输入您的意见和建议....',
                  padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                  onTextChange: (text) {

                  },
                  onSubmit: (text) {
                    print(text);
                  },
                ),
                Container(
                  child: Row(children: [
                    GestureDetector(
                        onTap: () {
                          selectImages();
                        },
                        child: Container(
                          margin: const EdgeInsets.all(12.0),
                          child: listFilePaths.isNotEmpty
                              ? Image.file(File(listFilePaths[0]['path']),
                                  width: 82, height: 82, fit: BoxFit.fill)
                              : Image.asset('images/sctp.png',
                                  width: 82, height: 82),
                        ))
                  ]),
                ),

              ],
            )));
  }
}
